<?php 
class Agency_userAction extends Action {
	public function index(){
		$model=M('Agency_user');
		$list=$model->select();
		
		
		$this->assign('list',$list);
		$this->display();
	}
	
	public function add_index(){
		$this->display();
	}
	
	public function add(){
		header('Content-Type:text/html; charset=utf-8');
		if ($_POST['submit']){
			
			if (empty($_POST['name'])||empty($_POST['password'])){
				$this->redirect('Agency_user/add_index', array(),1,'<script language="JavaScript">alert("用户名密码不能为空")</script>');
			}
			
			if ($_POST['password']!=$_POST['password2']){
				$this->redirect('Agency_user/add_index', array(),1,'<script language="JavaScript">alert("确认密码不相同")</script>');
			}
			//$map=array();
			$_POST['agency_user_name']=trim($_POST['name']);
			$_POST['agency_user_ps']=md5(trim($_POST['password'])."&abc");
			
			
			
		$model = M('Agency_user');
		$vo=$model->create();
		if ($vo) {
            if (false !== $model->add()) {
                $this->redirect('Agency_user/index', array(),1,'<script language="JavaScript">alert("添加成功")</script>');
            } else {
                $this->error("添加失败");
            }
        } else {
            header("Content-Type:text/html; charset=utf-8");
            $this->error($model->getError().' [ <a href="javascript:history.back()">return</a> ]');
            exit();
        }
			
		}
		
		$this->display();
	}
	
	public function edit_index(){
		if($_GET['agency_user_id']){
			$id=$_GET['agency_user_id'];
			$model2=M('Agency_user');
			$model2->getByAgency_user_id($id);
			$name=$model2->agency_user_name;
			//$pwd=$model2->password;
			$quanxian=$model2->authority;
		}
		
		$this->assign('agency_user_id',$id);
		$this->assign('agency_user_name',$name);
		//$this->assign('password',$pwd);
		$this->display();
	}
	
	public function edit(){
	if ($_POST['submit']){
			$whereid=trim($_POST['agency_user_id']);
			$map=array();
			$model1=M('Agency_user');
			$model1->getById($_POST['agency_user_id']);
			$name=$model1->agency_user_name;
			$pwd=$model1->password;
			unset($_POST['agency_user_id']);
			if($name!=trim($_POST['agency_user_name'])){
				$map['agency_user_name']=trim($_POST['agency_user_name']);
			}
			if(trim($_POST['password'])!=""){
				$map['agency_user_ps']=md5(trim($_POST['password'])."&abc");
			}

			$model=M('Agency_user');
			if (false !==$model->where('agency_user_id='.$whereid)->save($map)){
				$this->redirect('Agency_user/index', array(),1,'<script language="JavaScript">alert("修改成功")</script>');
			}else {
                $this->error("修改失败");
            }
		}
	}
}

?>